<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./test.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script>
      const app = document.getElementById('app');
      const shadowRoot = app.attachShadow({ mode: 'open' });

      // 样式
      const style = document.createElement('style');
      style.textContent = `
      .box {
        width: 100px;
        height: 100px;
        background: #000;
      }
      `;
      shadowRoot.appendChild(style);

      // 内容
      const div = document.createElement('div');
      div.className = 'box';
      shadowRoot.appendChild(div);
      // 监听事件
      shadowRoot.addEventListener('click', e => {
        console.log('click');
      });
    </script>
  </body>
</html>
